<template><div><h2 id="本地化" tabindex="-1"><a class="header-anchor" href="#本地化"><span>本地化</span></a></h2>
<ul>
<li><a href="#introduction">简介</a>
<ul>
<li><a href="#publishing-the-language-files">发布语言文件</a></li>
<li><a href="#configuring-the-locale">配置语言环境</a></li>
<li><a href="#pluralization-language">多语种</a></li>
</ul>
</li>
<li><a href="#defining-translation-strings">定义翻译字符串</a>
<ul>
<li><a href="#using-short-keys">使用短键</a></li>
<li><a href="#using-translation-strings-as-keys">使用翻译字符串作为键</a></li>
</ul>
</li>
<li><a href="#retrieving-translation-strings">检索翻译字符串</a>
<ul>
<li><a href="#replacing-parameters-in-translation-strings">替换翻译字符串中的参数</a></li>
<li><a href="#pluralization">复数化</a></li>
</ul>
</li>
<li><a href="#overriding-package-language-files">覆盖扩展包的语言文件</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<blockquote>
<p><strong>技巧</strong><br>
默认情况下，Laravel 应用程序框架不包含 <code v-pre>lang</code> 目录。如果你想自定义 Laravel 的语言文件，可以通过 <code v-pre>lang:publish</code> Artisan 命令发布它们。</p>
</blockquote>
<p>Laravel 的本地化功能提供了一种方便的方法来检索各种语言的字符串，从而使你可以轻松地在应用程序中支持多种语言。</p>
<p>Laravel 提供了两种管理翻译字符串的方法。首先，语言字符串可以存储在 <code v-pre>lang</code> 目录里的文件中。在此目录中，可能存在应用程序支持的每种语言的子目录。这是 Laravel 用于管理内置 Laravel 功能（例如验证错误消息）的翻译字符串的方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">/</span>lang</span>
<span class="line">    <span class="token operator">/</span>en</span>
<span class="line">        messages<span class="token operator">.</span>php</span>
<span class="line">    <span class="token operator">/</span>es</span>
<span class="line">        messages<span class="token operator">.</span>php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，可以在 <code v-pre>lang</code> 目录中放置的 JSON 文件中定义翻译字符串。采用这种方法时，应用程序支持的每种语言在此目录中都会有一个对应的 JSON 文件。对于具有大量可翻译字符串的应用，建议使用此方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">/</span>lang</span>
<span class="line">    en<span class="token operator">.</span>json</span>
<span class="line">    es<span class="token operator">.</span>json</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>我们将在本文档中讨论每种管理翻译字符串的方法。</p>
<h3 id="发布语言文件" tabindex="-1"><a class="header-anchor" href="#发布语言文件"><span>发布语言文件</span></a></h3>
<p>默认情况下，Laravel 应用程序框架不包含 <code v-pre>lang</code> 目录。如果你想自定义 Laravel 的语言文件或创建自己的语言文件，则应通过 <code v-pre>lang:publish</code> Artisan 命令构建 <code v-pre>lang</code> 目录。 <code v-pre>lang:publish</code> 命令将在应用程序中创建 <code v-pre>lang</code> 目录，并发布 Laravel 使用的默认语言文件集：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan lang:publish</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="配置语言环境" tabindex="-1"><a class="header-anchor" href="#配置语言环境"><span>配置语言环境</span></a></h3>
<p>应用程序的默认语言存储在 <code v-pre>config/app.php</code> 配置文件的 <code v-pre>locale</code> 配置选项中。你可以随意修改此值以适合你的应用程序的需求。</p>
<p>你可以使用 <code v-pre>App</code> Facade 提供的 <code v-pre>setLocale</code> 方法，在运行时通过单个 HTTP 请求修改默认语言：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>App</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/greeting/{locale}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$locale</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span> <span class="token function">in_array</span><span class="token punctuation">(</span><span class="token variable">$locale</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'en'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'es'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'fr'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token function">abort</span><span class="token punctuation">(</span><span class="token number">400</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">App</span><span class="token operator">::</span><span class="token function">setLocale</span><span class="token punctuation">(</span><span class="token variable">$locale</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以配置一个 「备用语言」，当当前语言不包含给定的翻译字符串时，将使用该语言。和默认语言一样，备用语言也是在 config/app.php 配置文件中配置的。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'fallback_locale'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'en'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="确定当前的语言环境" tabindex="-1"><a class="header-anchor" href="#确定当前的语言环境"><span>确定当前的语言环境</span></a></h4>
<p>你可以使用 <code v-pre>currentLocale</code> 和 <code v-pre>isLocale</code> 方法来确定当前的 <code v-pre>locale</code> 或检查 <code v-pre>locale</code> 是否是一个给定值。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>App</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$locale</span> <span class="token operator">=</span> <span class="token class-name static-context">App</span><span class="token operator">::</span><span class="token function">currentLocale</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">App</span><span class="token operator">::</span><span class="token function">isLocale</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'en'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="多语种" tabindex="-1"><a class="header-anchor" href="#多语种"><span>多语种</span></a></h3>
<p>你可以使用 Laravel 的「pluralizer」来使用英语以外的语言，Eloquent 和框架的其他部分使用它来将单数字字符串转为复数字符串。这可以通过调用应用程序服务提供的 <code v-pre>boot</code> 方法中的 <code v-pre>useLanguage</code> 方法来实现。复数器目前支持的语言有 <code v-pre>法语</code>, <code v-pre>挪威语</code>, <code v-pre>葡萄牙语</code>, <code v-pre>西班牙语</code>, <code v-pre>土耳其语</code>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Pluralizer</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用程序服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Pluralizer</span><span class="token operator">::</span><span class="token function">useLanguage</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'spanish'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>     </span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...     </span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
如果你想自定义 pluralizer 的语言，则应该明确定义 Elquent 模型的 <a href="https://learnku.com/docs/laravel/10.x/eloquentmd#table-names" target="_blank" rel="noopener noreferrer">表名</a>。</p>
</blockquote>
<h2 id="定义翻译字符串" tabindex="-1"><a class="header-anchor" href="#定义翻译字符串"><span>定义翻译字符串</span></a></h2>
<h3 id="使用短键" tabindex="-1"><a class="header-anchor" href="#使用短键"><span>使用短键</span></a></h3>
<p>通常，翻译字符串存储在 <code v-pre>lang</code> 目录中的文件中。在这个目录中，应用程序支持的每种语言都应该有一个子目录。这是 Laravel 用于管理内置 Laravel 功能（如验证错误消息）的翻译字符串的方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">/</span>lang</span>
<span class="line">    <span class="token operator">/</span>en</span>
<span class="line">        messages<span class="token operator">.</span>php</span>
<span class="line">    <span class="token operator">/</span>es</span>
<span class="line">        messages<span class="token operator">.</span>php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>所有的语言文件都会返回一个键值对数组。比如下方这个例子：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// lang/en/messages.php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'welcome'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Welcome to our application!'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>技巧</strong><br>
对于不同地区的语言，应根据 ISO 15897 命名语言目录。例如，英式英语应使用「en_GB」而不是 「en_gb」。</p>
</blockquote>
<h3 id="使用翻译字符串作为键" tabindex="-1"><a class="header-anchor" href="#使用翻译字符串作为键"><span>使用翻译字符串作为键</span></a></h3>
<p>对于具有大量可翻译字符串的应用程序，在视图中引用键时，使用「短键」定义每个字符串可能会令人困惑，并且为应用程序支持的每个翻译字符串不断发明键会很麻烦。</p>
<p>出于这个原因，Laravel 还支持使用字符串的「默认」翻译作为键来定义翻译字符串。使用翻译字符串作为键的翻译文件作为 JSON 文件存储在 <code v-pre>lang</code> 目录中。例如，如果你的应用程序有西班牙语翻译，你应该创建一个 <code v-pre>lang/es.json</code> 文件：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"I love programming."</span><span class="token operator">:</span> <span class="token string">"Me encanta programar."</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="键-文件冲突" tabindex="-1"><a class="header-anchor" href="#键-文件冲突"><span>键 / 文件冲突</span></a></h4>
<p>你不应该定义和其他翻译文件的文件名存在冲突的键。例如，在 <code v-pre>nl/action.php</code> 文件存在，但 <code v-pre>nl.json</code> 文件不存在时，对 <code v-pre>NL</code> 语言翻译 <code v-pre>__('Action')</code> 会导致翻译器返回 <code v-pre>nl/action.php</code> 文件的全部内容。</p>
<h2 id="检索翻译字符串" tabindex="-1"><a class="header-anchor" href="#检索翻译字符串"><span>检索翻译字符串</span></a></h2>
<p>你可以使用 <code v-pre>__</code> 辅助函数从语言文件中检索翻译字符串。 如果你使用 「短键」 来定义翻译字符串，你应该使用 「.」 语法将包含键的文件和键本身传递给<code v-pre>__</code>函数。 例如，让我们从 <code v-pre>lang/en/messages.php</code> 语言文件中检索 <code v-pre>welcome</code> 翻译字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果指定的翻译字符串不存在，<code v-pre>__</code> 函数将返回翻译字符串键。 因此，使用上面的示例，如果翻译字符串不存在，<code v-pre>__</code> 函数将返回 <code v-pre>messages.welcome</code>。</p>
<p>如果是使用 <a href="#using-translation-strings-as-keys">默认翻译字符串作为翻译键</a>，则应将字符串的默认翻译传递给 <code v-pre>__</code> 函数；</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'I love programming.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>同理，如果翻译字符串不存在，<code v-pre>__</code> 函数将返回给定的翻译字符串键。</p>
<p>如果是使用的是 <a href="https://learnku.com/docs/laravel/10.x/blade" target="_blank" rel="noopener noreferrer">Blade 模板引擎</a>，则可以使用 <code v-pre>{{ }}</code> 语法来显示翻译字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="替换翻译字符串中的参数" tabindex="-1"><a class="header-anchor" href="#替换翻译字符串中的参数"><span>替换翻译字符串中的参数</span></a></h3>
<p>如果愿意，可以在翻译字符串中定义占位符。所有占位符的前缀都是 <code v-pre>:</code>。例如，可以使用占位符名称定义欢迎消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'welcome'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Welcome, :name'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>在要检索翻译字符串时替换占位符，可以将替换数组作为第二个参数传递给 <code v-pre>__</code> 函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'dayle'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果占位符包含所有大写字母，或仅首字母大写，则转换后的值将相应地转换成大写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'welcome'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Welcome, :NAME'</span><span class="token punctuation">,</span> <span class="token comment">// Welcome, DAYLE</span></span>
<span class="line"><span class="token string single-quoted-string">'goodbye'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Goodbye, :Name'</span><span class="token punctuation">,</span> <span class="token comment">// Goodbye, Dayle</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="对象替换格式" tabindex="-1"><a class="header-anchor" href="#对象替换格式"><span>对象替换格式</span></a></h4>
<p>如果试图提供对象作为转换占位符，则将调用对象的 <code v-pre>__toString</code> 方法。<a href="https://www.php.net/manual/en/language.oop5.magic.php#object.tostring" target="_blank" rel="noopener noreferrer"><code v-pre>__toString</code></a>方法是PHP内置的「神奇方法」之一。然而，有时你可能无法控制给定类的 <code v-pre>__toString</code> 方法，例如当你正在交互的类属于第三方库时。</p>
<p>在这些情况下，Laravel 允许你为特定类型的对象注册自定义格式处理程序。要实现这一点，你应该调用转换器的 <code v-pre>stringable</code> 方法。 <code v-pre>stringable</code> 方法接受闭包，闭包应类型提示其负责格式化的对象类型。通常，应在应用程序的 <code v-pre>AppServiceProvider</code> 类的 <code v-pre>boot</code> 方法中调用 <code v-pre>stringable</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Lang</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Money<span class="token punctuation">\</span>Money</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用程序服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Lang</span><span class="token operator">::</span><span class="token function">stringable</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Money</span> <span class="token variable">$money</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$money</span><span class="token operator">-></span><span class="token function">formatTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'en_GB'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="复数化" tabindex="-1"><a class="header-anchor" href="#复数化"><span>复数化</span></a></h3>
<p>因为不同的语言有着各种复杂的复数化规则，所以复数化是个复杂的问题；不过 Laravel 可以根据你定义的复数化规则帮助你翻译字符串。使用 <code v-pre>|</code> 字符，可以区分字符串的单数形式和复数形式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'apples'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'There is one apple|There are many apples'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>当然，使用 <a href="#using-translation-strings-as-keys">翻译字符串作为键</a> 时也支持复数化：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"There is one apple|There are many apples"</span><span class="token operator">:</span> <span class="token string">"Hay una manzana|Hay muchas manzanas"</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你甚至可以创建更复杂的复数化规则，为多个值范围指定转换字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'apples'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'{0} There are none|[1,19] There are some|[20,*] There are many'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>定义具有复数选项的翻译字符串后，可以使用 <code v-pre>trans_choice</code> 函数检索给定「count」的行。在本例中，由于计数大于 1 ，因此返回翻译字符串的复数形式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">trans_choice</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.apples'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>也可以在复数化字符串中定义占位符属性。通过将数组作为第三个参数传递给 <code v-pre>trans_choice</code> 函数，可以替换这些占位符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'minutes_ago'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'{1} :value minute ago|[2,*] :value minutes ago'</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token function">trans_choice</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'time.minutes_ago'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果要显示传递给 <code v-pre>trans_choice</code> 函数的整数值，可以使用内置的 <code v-pre>:count</code> 占位符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'apples'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'{0} There are none|{1} There is one|[2,*] There are :count'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="覆盖扩展包的语言文件" tabindex="-1"><a class="header-anchor" href="#覆盖扩展包的语言文件"><span>覆盖扩展包的语言文件</span></a></h2>
<p>有些包可能随自己的语言文件一起封装。你可以将文件放置在 <code v-pre>lang/vendor/{package}/{locale}</code> 目录中，而不是更改扩展包的核心文件来调整这些行。</p>
<p>例如，如果需要重写位于名为 <code v-pre>skyrim/hearthfire</code> 的包的 <code v-pre>messages.php</code> 文件内容，应将语言文件放在： <code v-pre>lang/vendor/hearthfire/en/messages.php</code> 在这个文件中，你应该只定义要覆盖的翻译字符串。任何未重写的翻译字符串仍将从包的原始语言文件中加载。</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/10.x/localization/14868" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/lo...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/10.x/localization/14868" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/lo...</a></p>
</blockquote>
</div></template>


